package com.nondo.dean.platform.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.Slide;
import com.nondo.dean.platform.domain.SlideExample;
import com.nondo.dean.platform.mapper.SlideMapper;
import com.nondo.dean.platform.model.SlideBean;
import com.nondo.dean.platform.model.result.ProductResult;
import com.nondo.dean.platform.model.result.SlideResult;

/**
 * 幻灯片管理业务类
 * 
 * @author zhaoljin
 * @date 2018年3月27日
 */
@Component
public class SlideService {

    @Autowired
    private AliyunOssTool aliyunOssTool;
    
    @Autowired
    private SlideMapper slideMapper;
    
    @Autowired
    private ProductService productService;

    /**
     * 插入幻灯片
     * @param slide
     * @return
     */
    @Transactional
    public int addSlide(Slide slide) {
        return slideMapper.insertSelective(slide);
    }

    /**
     * 修改幻灯片
     * @param slide
     * @return
     */
    @Transactional
    public int updateSlide(Slide slide) {
        return slideMapper.updateByPrimaryKeySelective(slide);
    }

    /**
     * 按照主键ID查询幻灯片
     * @param id
     * @return
     */
    public Slide selectSlide(Integer id) {
        return slideMapper.selectByPrimaryKey(id);
    }

    /**
     * 幻灯片分页查询列表
     * @param offset
     * @param limit
     * @param slide
     * @return
     */
    public List<Slide> getSlideList(int offset, int limit, Slide slide) {
        SlideExample example = new SlideExample();
        example.setOrderByClause("order_num ASC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        SlideExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, slide);
        return slideMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 幻灯片总数统计
     * @param slide
     * @return
     */
    public Long count(Slide slide) {
        SlideExample example = new SlideExample();
        SlideExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, slide);
        return slideMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param slide
     */
    void setCriteria(SlideExample.Criteria criteria, Slide slide) {
        if (slide != null) {
            if (slide.getId() != null)
                criteria.andIdEqualTo(slide.getId());
            if (slide.getPosition() != null)
                criteria.andPositionEqualTo(slide.getPosition());
            if (StringUtils.isNoneBlank(slide.getName()))
                criteria.andNameLike("%" + slide.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照幻灯片名称查询幻灯片
    * @param name
    * @return
    */
    public Slide selectSlideByName(String name) {
        SlideExample example = new SlideExample();
        SlideExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Slide> slideList = slideMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(slideList)?slideList.get(0):null;
    }
    
    /**
     * 幻灯片组装信息
     * @param slide
     * @return {@link SlideBean}
     */
    public SlideBean getSlideBean(Slide slide) {
        SlideBean slideBean = SlideBean.covertToBean(slide);
        slideBean.setImage(aliyunOssTool.getImgUrl(slideBean.getImage()));
        return slideBean;
    }
    
    /**
     * 幻灯片列表
     * @param 位置：0.经销商首页1.商品采购首页2.微信小程序
     * @return 不分页的幻灯片列表
     */
    public List<Slide> getSlideList(Integer position) {
        SlideExample example = new SlideExample();
        example.setOrderByClause("order_num ASC");
        SlideExample.Criteria criteria = example.createCriteria();
        if (position != null) {
            criteria.andPositionEqualTo(position);
        }
        criteria.andDelFlagEqualTo(Slide.DEL_FLAG_NORMAL);
        return slideMapper.selectByExample(example);
    }

    /**
     * 获取包含扩展参数的幻灯片的详情
     * @param slide {@link Slide}
     * @return {@link SlideResult}
     */
    public SlideResult getSlideDetail(Slide slide) {
        SlideResult slideResult = SlideResult.covertToBean(slide);
        slideResult.setImage(aliyunOssTool.getImgUrl(slide.getImage()));
        Integer type = slide.getType();
        String value = slide.getValue();
        ProductResult productResult = new ProductResult();
        List<ProductResult> productList = new ArrayList<ProductResult>();
        switch (type) {
            case 10: // 产品列表
                productList = productService.getProductBeanListByIds(value);
                break;
            case 11: // 产品详情
                productResult = productService.getProductBeanById(Integer.parseInt(value));
                productList.add(productResult);
                break;
            default:
                break;
        }
        slideResult.setExtra(productList);
        return slideResult;
    }
    
}
